package dao.daoimpl;

import util.DataBase;
import dao.LikeDao;
import bean.Like;
import bean.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class LikeDaoImpl implements LikeDao {
    @Override
    public boolean addLike(Like like) {
        String sql="insert into like_table (like_articleId,like_user) values ('"+like.getArticleId()+"','"+like.getUser()+"')";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteLike(int id) {
        String sql="delete from like_table where like_id='"+id+"'";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public ArrayList<Like> getLikes(User user) throws SQLException {
        ArrayList<Like> likes=new ArrayList<>();
        String sql="select * from like_table where like_user='"+user.getAccount()+"'";
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()){
            int id=resultSet.getInt("like_id");
            int articleId=resultSet.getInt("like_articleId");
            String user1=resultSet.getString("like_user");
            likes.add(new Like(id,articleId,user1));
        }
        DataBase.release();
        return likes;
    }
}
